﻿using System;
using System.Collections.Generic;
using System.Text;
using TarreOA.TarreOAModels;
using TarreOA.TarreOADAL;

namespace TarreOA.TarreOABLL
{
    public class AfficheInformtbService
    {
        /// <summary>
        /// 新建公告通知信息
        /// </summary>
        /// <param name="aiTitle"></param>
        /// <param name="aiContent"></param>
        /// <param name="aiCreateTime"></param>
        /// <param name="aiStartTime"></param>
        /// <param name="aiEndTime"></param>
        /// <param name="aiAffixFilePath"></param>
        /// <param name="aiOpenBound"></param>
        /// <returns></returns>
        public int[] saveAfficheInformInfo(string aiTitle, string aiContent, DateTime aiCreateTime, DateTime aiStartTime, DateTime aiEndTime,
            string aiAffixFilePath, string aiOpenBound, int userId)
        {
            AfficheInformtbDAO aiDAO = new AfficheInformtbDAO();
            if(",0".Equals(aiOpenBound)) {
                SectionDAO sectionDAO = new SectionDAO();
                string openBound = ",";
                List<Section> sections = sectionDAO.findAllSections();
                foreach(Section section in sections) {
                    openBound += (section.SectionId + ",");
                }
                aiOpenBound = openBound;
            }
            return aiDAO.saveAfficheInformInfo(aiTitle, aiContent, aiCreateTime, aiStartTime, aiEndTime, aiAffixFilePath, aiOpenBound, userId);
        }

        /// <summary>
        /// 根据公告通知编号查找公告通知信息
        /// </summary>
        /// <param name="ai_id"></param>
        /// <returns></returns>
        public AfficheInformtb findById(int ai_id)
        {
            AfficheInformtbDAO aiDAO = new AfficheInformtbDAO();
            SectionDAO sectionDAO = new SectionDAO();
            AfficheInformtb ai = aiDAO.findById(ai_id);
            if(!"".Equals(ai.AfficheInformOpenBound)) {
                string[] sections = ai.AfficheInformOpenBound.Split(',');
                ai.AfficheInformOpenBoundSnames = "";
                if("0".Equals(sections[0])) {
                    ai.AfficheInformOpenBoundSnames = "所有部门";
                } else {
                    for(int i = 0; i < sections.Length; i++) {
                        if(sections[i] != null && !"".Equals(sections[i])) {
                            Section section = sectionDAO.findBySectionId(int.Parse(sections[i]));
                            ai.AfficheInformOpenBoundSnames += (section.SectionName + "<br />");
                        }
                    }
                }
            } else {
                ai.AfficheInformOpenBoundSnames = "暂不开放";
            }
            return ai;
        }

        /// <summary>
        /// 根据公告通知编号更改公告通知信息
        /// </summary>
        /// <param name="aiTitle"></param>
        /// <param name="aiContent"></param>
        /// <param name="aiCreateTime"></param>
        /// <param name="aiStartTime"></param>
        /// <param name="aiEndTime"></param>
        /// <param name="aiAffixFilePath"></param>
        /// <param name="aiOpenBound"></param>
        /// <param name="aiid"></param>
        /// <returns></returns>
        public int updateAfficheInformInfo(string aiTitle, string aiContent, DateTime aiCreateTime, DateTime aiStartTime, DateTime aiEndTime,
            string aiAffixFilePath, string aiOpenBound, int aiid)
        {
            AfficheInformtbDAO aiDAO = new AfficheInformtbDAO();
            return aiDAO.updateAfficheInformInfo(aiTitle, aiContent, aiCreateTime, aiStartTime, aiEndTime, aiAffixFilePath, aiOpenBound, aiid);
        }

        /// <summary>
        /// 根据公告通知编号删除公告通知信息
        /// </summary>
        /// <param name="aiid"></param>
        /// <returns></returns>
        public int delAfficheInformInfoById(int aiid)
        {
            AfficheInformtbDAO aiDAO = new AfficheInformtbDAO();
            return aiDAO.delAfficheInformInfoById(aiid);
        }

        /// <summary>
        /// 查找所有公告通知信息
        /// </summary>
        /// <returns></returns>
        public List<AfficheInformtb> findALL()
        {
            AfficheInformtbDAO aiDAO = new AfficheInformtbDAO();
            List<AfficheInformtb> list = aiDAO.findALL();
            SectionDAO sectionDAO = new SectionDAO();
            if (list != null && list.Count > 0)
            {
                foreach (AfficheInformtb ai in list)
                {
                    if (!"".Equals(ai.AfficheInformOpenBound))
                    {
                        string[] sections = ai.AfficheInformOpenBound.Split(',');
                        ai.AfficheInformOpenBoundSnames = "";
                        if ("0".Equals(sections[0]))
                        {
                            ai.AfficheInformOpenBoundSnames = "所有部门";
                        }
                        else
                        {
                            for (int i = 0; i < sections.Length; i++)
                            {
                                if (sections[i] != null && !"".Equals(sections[i]))
                                {
                                    Section section = sectionDAO.findBySectionId(int.Parse(sections[i]));
                                    ai.AfficheInformOpenBoundSnames += (section.SectionName + "<br />");
                                }
                            }
                        }
                    }
                    else
                    {
                        ai.AfficheInformOpenBoundSnames = "暂不开放";
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 查找指定日期内的公告通知
        /// </summary>
        /// <param name="startDate"></param>
        /// <returns></returns>
        public List<AfficheInformtb> findBetweenStartDateAndEndDate(DateTime startDate)
        {
            AfficheInformtbDAO aiDAO = new AfficheInformtbDAO();
            return aiDAO.findBetweenStartDateAndEndDate(startDate);
        }

         /// <summary>
        /// 根据公告通知编号让该通知立即终止
        /// </summary>
        /// <param name="ai_id"></param>
        /// <returns></returns>
        public int toStopByAiid(int ai_id) {
            AfficheInformtbDAO aiDAO = new AfficheInformtbDAO();
            return aiDAO.toStopByAiid(ai_id);
        }

        /// <summary>
        /// 根据公告通知编号让该通知恢复生效
        /// </summary>
        /// <param name="ai_id"></param>
        /// <returns></returns>
        public int toGoOnByAiid(int ai_id) {
            AfficheInformtbDAO aiDAO = new AfficheInformtbDAO();
            return aiDAO.toGoOnByAiid(ai_id);
        }

        /// <summary>
        /// 根据公告通知编号查看该通知各部门人员的查看情况
        /// </summary>
        /// <param name="ai_id"></param>
        /// <returns></returns>
        public List<AfficheInformConsulttb> findConsultByAiid(int ai_id) {
            List<AfficheInformConsulttb> list = null;
            SectionDAO sectionDAO = new SectionDAO();
            UserDAO userDAO = new UserDAO();
            AfficheInformtb ai = this.findById(ai_id);
            string sectionsid = ai.AfficheInformOpenBound;
            if(sectionsid != null && !"".Equals(sectionsid)) {
                string[] sids = sectionsid.Split(',');
                list = new List<AfficheInformConsulttb>();
                List<User> finishUsers = ai.AfficheInformFinishUsers;
                List<User> unfinishUsers = ai.AfficheInformUnFinishUsers;
                for(int i = 0; i < sids.Length; i++) {
                    if(sids[i] != null && !"".Equals(sids[i])) {
                        AfficheInformConsulttb aiConsult = new AfficheInformConsulttb();
                        aiConsult.SectionId = int.Parse(sids[i]);
                        aiConsult.SectionName = sectionDAO.findBySectionId(int.Parse(sids[i])).SectionName;
                        if(finishUsers != null && finishUsers.Count > 0) {
                            foreach(User finishUser in finishUsers) {
                                if(finishUser.Section.SectionId == int.Parse(sids[i])) {
                                    aiConsult.FinishUsernames += (finishUser.UserName + ",");
                                }
                            }
                        }
                        if(unfinishUsers != null && unfinishUsers.Count > 0) {
                            foreach(User unfinishUser in unfinishUsers) {
                                if(unfinishUser.Section.SectionId == int.Parse(sids[i])) {
                                    aiConsult.UnFinishUsernames += (unfinishUser.UserName + ",");
                                }
                            }
                        }
                        list.Add(aiConsult);
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 跟据当前用户所属部门查看对应的公告通知
        /// </summary>
        /// <param name="usid"></param>
        /// <returns></returns>
        public List<AfficheInformtb> findByUser(int usid) {
            AfficheInformtbDAO aiDAO = new AfficheInformtbDAO();
            return aiDAO.findByUser(usid);
        }

        /// <summary>
        /// 根据公告通知编号更改该通知的已阅读和未阅读用户的状态
        /// </summary>
        /// <param name="ai_id"></param>
        /// <param name="finishUsersid"></param>
        /// <param name="unfinishUsersid"></param>
        /// <returns></returns>
        public int updateFinishAndUnFinishUsersid(int ai_id, string finishUsersid, string unfinishUsersid) {
            AfficheInformtbDAO aiDAO = new AfficheInformtbDAO();
            return aiDAO.updateFinishAndUnFinishUsersid(ai_id, finishUsersid, unfinishUsersid);
        }
    }
}
